/*
 * Copyright 2013 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.yunos.tv.common.util;


import java.util.HashSet;
import java.util.Set;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * 类PinyinUtil.java的实现描述：TODO 类实现描述
 * @author riqi 2013-7-1 下午3:52:13
 */
public class PinyinUtil {

    /**
     * 获取拼音集合
     * @author wyh
     * @param src
     * @return Set<String>
     */
    public static Set<String> getPinyin(String src) {
        if (src != null && !src.trim().equalsIgnoreCase("")) {
            char[] srcCharArray = src.toCharArray();
            String[][] temp = new String[src.length()][];

            for (int i = 0; i < srcCharArray.length; i++) {
                char srcChar = srcCharArray[i];

                if (String.valueOf(srcChar).matches("[\\u4E00-\\u9FA5]+")) {
                    try {
                        temp[i] = PinyinHelper.toHanyuPinyinStringArray(srcCharArray[i], pinYinOutputFormat());
                    } catch (BadHanyuPinyinOutputFormatCombination e) {
                        e.printStackTrace();
                    }
                } else if (((int) srcChar >= 65 && (int) srcChar <= 90) || ((int) srcChar >= 97 && (int) srcChar <= 122)) {
                    temp[i] = new String[] { String.valueOf(srcCharArray[i]) };
                } else {
                    temp[i] = new String[] { "" };
                }
            }

            Set<String> pinyinSet = new HashSet<String>();
            String[] pingyinArray = Exchange(temp);
            for (int i = 0; i < pingyinArray.length; i++) {
                pinyinSet.add(pingyinArray[i]);
            }

            return pinyinSet;
        }

        return null;
    }

    public static String[] Exchange(String[][] strJaggedArray) {
        String[][] temp = DoExchange(strJaggedArray);
        return temp[0];
    }

    private static String[][] DoExchange(String[][] strJaggedArray) {
        int len = strJaggedArray.length;
        if (len >= 2) {
            int len1 = strJaggedArray[0].length;
            int len2 = strJaggedArray[1].length;
            int newlen = len1 * len2;
            String[] temp = new String[newlen];
            int Index = 0;
            for (int i = 0; i < len1; i++) {
                for (int j = 0; j < len2; j++) {
                    temp[Index] = strJaggedArray[0][i] + strJaggedArray[1][j];
                    Index++;
                }
            }
            String[][] newArray = new String[len - 1][];
            for (int i = 2; i < len; i++) {
                newArray[i - 1] = strJaggedArray[i];
            }
            newArray[0] = temp;

            return DoExchange(newArray);
        } else {
            return strJaggedArray;
        }
    }

    //汉语拼音格式输出类  
    private static final HanyuPinyinOutputFormat pinYinOutputFormat() {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();

        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);

        return format;
    }

    /**
     * demo
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(PinyinUtil.getPinyin("嘉兴学院"));
    }
}
